package io.wilson.rabbit.receiver;

import com.rabbitmq.client.Channel;
import io.wilson.rabbit.common.constant.RabbitMQConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
//public class RabbitReceiver implements BatchMessageListener {
public class RabbitReceiver {

    @RabbitListener(queues = "sequence-direct-queue-0",ackMode = "MANUAL", containerFactory = "rabbitListenerContainerFactory", concurrency = "1-10")
    public void handleDirect0(List<Message> messages, Channel channel) throws IOException {
        try {
            // 批量处理消息
            List<String> contents = new ArrayList<>(messages.size());
            for (Message msg : messages) {
                // Your message processing logic here
                contents.add(new String(msg.getBody(), StandardCharsets.UTF_8));
            }
            log.info("Received: {}",contents);
            messages.forEach(e -> {
                try {
                    channel.basicAck(e.getMessageProperties().getDeliveryTag(), true);
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            });
            // 批量确认消息
        } catch (Exception e) {
                channel.basicNack(messages.size(), true, false); // Nack last known tag, multiple=true, requeue=false
            e.printStackTrace();
        }
    }
/*    @RabbitListener(queues = "sequence-direct-queue-0")
    public void handleDirect0(List<Message> messages, Channel channel) throws IOException {
        try {
            // 批量处理消息
            log.info("Received: {}", messages);
            // 批量确认消息
        } catch (Exception e) {
                channel.basicNack(messages.size() -1, true, false); // Nack last known tag, multiple=true, requeue=false
            e.printStackTrace();
        }
    }*/

/*    @Override
    @RabbitListener(queues = "sequence-direct-queue-0", ackMode = "MANUAL", containerFactory = "rabbitListenerContainerFactory")
    public void onMessageBatch(List<Message> messages) {
        log.info("Received: {}", messages.stream()
                .map(e -> new String(e.getBody(), StandardCharsets.UTF_8))
                .collect(Collectors.toList()));
    }*/

    @RabbitListener(queues = "sequence-direct-queue-1")
    public void handleDirect1(String msg) {
        log.info("handleDirect1 queue[{}] msg: {}", RabbitMQConstant.SEQUENCE_DIRECT_QUEUE, msg);
    }

    @RabbitHandler
    @RabbitListener(queues = "sequence-direct-queue-2")
    public void handleDirect2(String msg) {
        log.info("handleDirect2 queue[{}] msg: {}", RabbitMQConstant.SEQUENCE_DIRECT_QUEUE, msg);
    }


    /*    @RabbitHandler
    @RabbitListener(queues = RabbitMQConstant.SEQUENCE_DIRECT_QUEUE)
    public void handleDirect3(String msg) {
        log.info("handleVocMsg queue[{}] msg: {}", RabbitMQConstant.SEQUENCE_DIRECT_QUEUE, msg);
    }*/
/*

    @RabbitHandler
    @RabbitListener(queues = RabbitMQConstant.TOPIC_QUEUE_NAME_1)
    public void handleTopicMsgA(String msg) {
        log.info("handleTopicMsgA queue[{}] msg: {}", RabbitMQConstant.TOPIC_QUEUE_NAME_1, msg);
    }

    @RabbitHandler
    @RabbitListener(queues = RabbitMQConstant.TOPIC_QUEUE_NAME_2)
    public void handleTopicMsgB(String msg) {
        log.info("handleTopicMsgB queue[{}] msg: {}", RabbitMQConstant.TOPIC_QUEUE_NAME_2, msg);
    }

    @RabbitHandler
    @RabbitListener(queues = RabbitMQConstant.TOPIC_QUEUE_NAME_3)
    public void handleTopicMsgC(String msg) {
        log.info("handleTopicMsgC queue[{}] msg: {}", RabbitMQConstant.TOPIC_QUEUE_NAME_3, msg);
    }
*/

}
